import sys, os, random
from commands import getoutput as g
import time
import shutil
import tempfile

########################
#   Global variables   #
########################

class Data:
	'''
	We use this class to store variables, which are used in this file:
	inputfilename, number of pages in a block, pagenumberng,
	filename, filepath, filetype, temporary dir name
	'''
	pass

data=Data()




########################
#      start point     #
########################

def process(inputfile, pages, numbering):
	'''
	In this script we perform  document processing itself.
	For the details see file logic-diagram.svg

	As the input we take $$.PS or $$.PDF file, then we create temporary folder
	and number pages, if necessary. After we use psbook to make paper blocks,
	psnup to place 2 pages to a new A4 page and place marks for punching
	on the page using pdftk. Result is converted in PDF and saved as $$-book.pdf
	'''
	# We save parameters
	data.inputfile = inputfile
	data.pages     = pages
	data.numbering = numbering

	# We determine filetype using extension (we trust it)
	if inputfile.upper().split('.')[-1] == "PDF":
	    data.filetype = "PDF"
	else:
	    data.filetype = "PS"

	# Get filename and filepath
	data.filename = inputfile.split('/')[-1]
	data.filepath = inputfile.split(data.filename)[0]
	

	start()
	if data.numbering:
		if data.filetype == 'PS':
			print('ps2pdf start.ps start.pdf')
			g('ps2pdf start.ps start.pdf')
		addNumbers()
		print('pdf2ps numbered.pdf 1.ps')
		g('pdf2ps numbered.pdf 1.ps')
	else:
		if data.filetype == 'PDF':
			print('pdf2ps start.pdf 1.ps')
			g('pdf2ps start.pdf 1.ps')
		else:
			print('copy \"start.ps\" to \"1.ps\"')
			shutil.copy('start.ps', '1.ps')
	print('psbook -s ' + str(data.pages) + ' 1.ps 2.ps')
	g('psbook -s ' + str(data.pages) + ' 1.ps 2.ps')
	print('psnup -2 -p 14 2.ps 3.ps')
	g('psnup -2 -p a4 2.ps 3.ps')
	print('ps2pdf 3.ps 4.pdf')
	g('ps2pdf 3.ps 4.pdf')
	addMarks()
#	print('FIXME! adding marks for punching')
	shutil.copy('5.pdf', '../' + data.filename + '-book.pdf')
	

	time.sleep(0) # Just for debugging
	os.chdir('..')
	g("rm -rf " + data.dirname)

	




########################
#   Helper functions   #
########################

def start():
	'''
	Creates temporary directory and file "start.pdf" or "start.ps" inside
	'''
	tempfile.tempdir = '.'
	data.dirname  = tempfile.mktemp()
	os.mkdir(data.dirname)
	if data.filetype == 'PDF':
		print ('copy \"' + data.inputfile + '\" to \"' + data.dirname + '/start.pdf\"')
		shutil.copy(data.inputfile, data.dirname + '/start.pdf')
	else:
		print ('copy \"' + data.inputfile + '\" to \"' + data.dirname + '/start.ps\"')
		shutil.copy(data.inputfile, data.dirname + '/start.ps')
	os.chdir(data.dirname)

def latexprepare():
	print ('FIXME! Prepearing LaTeX file and PDF')
	pass

def addNumbers():
	latexprepare()
	print ('FIXME! Adding numbers!')
	# Not it completely dummy!
	shutil.copy('start.pdf', 'numbered.pdf')
	pass

def addMarks():
	'''
	Adds marks for punching using pdftk from file auxilary/marks.pdf
	'''
	print ('pdftk 4.pdf multistamp ../auxilary/marks.pdf output 5.pdf')
	g('pdftk 4.pdf multistamp ../auxilary/marks.pdf output 5.pdf')















